<?php

namespace MoChat\App\Call\Service;

use Hyperf\DbConnection\Db;
use MoChat\App\Call\Constants\CallPlanIsExportNextTaskCode;
use MoChat\App\Call\Constants\ClientTimeCode;
use MoChat\App\Call\Constants\ClientTypeCode;
use MoChat\App\Call\Contract\CallPlanContract;
use MoChat\App\Call\Contract\CallPlanWorkContactContract;
use MoChat\App\Call\Model\CallPlan;
use MoChat\App\Call\Model\CallPlanWorkContact;
use MoChat\Framework\Service\AbstractService;

class CallPlanWorkContactService extends AbstractService implements CallPlanWorkContactContract
{

    /**
     * @var CallPlanWorkContact
     */
    protected $model;

    public function deleteContract($id): int
    {
        return $this->model->deleteOne($id);
    }

    public function createContract($data)
    {
        $this->model->createOne($data);
    }

    public function getIndexContract($where, array $filed = ['*']): \Hyperf\Contract\LengthAwarePaginatorInterface
    {
        $list = $this->model->getList($where, $filed);
        if (!empty($list->items())) {
            foreach ($list->items() as &$datum) {
                $datum['b2bUserTypeString'] = ClientTypeCode::getMessage($datum->b2b_user_type);
            }
            unset($datum);
        }
        return $list;
    }


    public function getOmissionContract($where, array $filed = ['*']): \Hyperf\Contract\LengthAwarePaginatorInterface
    {
        $list = $this->model->getOmissionList($where, $filed);
        if (!empty($list->items())) {
            $ids = array_column($list->items(), 'top_call_plan_id');
            $ids = array_unique(array_filter($ids));
            $callPlan = [];

            if (!empty($ids)) {
                $callPlan = CallPlan::whereIn('id', $ids)->pluck('plan_time', 'id')->toArray();
            }
            foreach ($list->items() as &$datum) {
                $datum['b2bUserTypeString'] = ClientTypeCode::getMessage($datum->b2b_user_type);
                $datum['statusString'] = $datum->status > 0 ? '已完成' : '未完成';
                $datum['timeOut'] = '0小时';
                if ($datum->is_omission && !empty($callPlan)) {
                    $key = $datum['top_call_plan_id'];
                    $seconds = time() - strtotime($callPlan[$key]);
                    $hour = ceil($seconds / 60);
                    $datum['timeOut'] = $hour . '小时';
                }
            }
            unset($datum);
        }
        return $list;
    }

    public function updateContract($id, $data): int
    {
        return $this->model->updateOneById($id, $data);
    }

    public function createAllContract($data)
    {
        $arr = [];
        $data['type'] = $data['type'] ?? ClientTimeCode::TODAY;
        foreach ($data['clientIds'] as $clientId) {
            $time = $data['type'] == ClientTimeCode::TODAY ? date('Y-m-d 00:00:00') : date('Y-m-d 00:00:00', strtotime("+1 day"));
            $arr[] = [
                'client_id' => $clientId,
                'call_plan_id' => $data['callPlanId'] ? $data['callPlanId'] : 0,
                'created_at' => $time
            ];
        }
        return $this->model->insert($arr);
    }

    public function transferTask($data)
    {
        foreach ($data['ids'] as $clientId) {
            CallPlanWorkContact::where('id', $clientId)->update([
                'call_plan_id' => $data['callPlanId'],
            ]);
        }
    }
}
